package cn.lyd.org.controller;

import cn.lyd.base.until.ExcelUtil;
import cn.lyd.base.until.LoginContext;
import cn.lyd.org.domain.ShopAuditLog;
import cn.lyd.org.dto.ShopAuditDto;
import cn.lyd.org.dto.ShopDto;
import cn.lyd.org.service.IShopService;
import cn.lyd.org.domain.Shop;
import cn.lyd.org.query.ShopQuery;
import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryByPage(query);
    }


    /**
     * 店铺入驻  @Valid 字段校验
     * @param dto
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopDto dto){
        try {
            shopService.settlement(dto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！");
        }
    }

    /**
     * 店铺审核通过
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody @Valid ShopAuditDto dto){
        try {
            shopService.auditPass(dto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！");
        }
    }

    /**
     * 店铺审核驳回
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody @Valid ShopAuditDto dto){
        try {
            shopService.auditReject(dto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后再试！");
        }
    }

    /**
     * 店铺激活
     */
    @GetMapping("/activate/{id}")
    public AjaxResult activate(@PathVariable("id") Long id){
        shopService.activate(id);
        return new AjaxResult();
    }

    /**
     * 店铺重新入驻
     */
    @PostMapping("/settlement/again")
    public AjaxResult registerAgain(@RequestBody @Valid ShopDto dto){
        shopService.registerAgain(dto);
        return new AjaxResult();
    }


    /**
     * 导出
     * @param res
     * @return
     *         MultipartFile file, //文件对象
     *         Integer titleRows,//忽略标题行数
     *         Integer headerRows,//忽略表头行数
     *         Class<T> pojoClass)//生成的对象类型
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse res){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",res);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
//        System.out.println(shops.size());
        //todo 调用批量新增方法，保存店铺数据
//        shopService.batchAdd(shops);

        //法二：遍历新增
        for (Shop shop : shops){
            shopService.add(shop);
        }

    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            shopService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 报表
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }


}
